SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TTSUBR_FINA_Q01]
/*---------------------------------------------------------*/
/*---- Empresa		   : OFISIS S.A				----*/
/*---- Cliente		   : OFISIS S.A.				----*/
/*---- Sistema		   : Tesoreria					----*/
/*---- Modulo		   : Empresa					----*/
/*---- Programa	   : Flujo de Caja Proyectado, Eje	----*/
/*---- Script		   : TTARUB_FINA_Q01.SQL			----*/
/*---- Desarrollado por	   : Carlos Marquez Escobar	----*/
/*---- Fecha Creacion	   : 05/01/2003			----*/
/*---- Base de Datos	   : SQL SERVER			----*/
/*---- Version		   : 8.0						----*/
/*---- Invoca a SP 	   :							----*/
/*---------------------------------------------------------*/
/*-- Comentarios	   : 						
CREATE TABLE #TWSUBR_FINA (
	CO_EMPR		Varchar(002)	Null,
	NU_ANNO		Int  		Null, 
	NU_MESE		Int  		Null,
	CO_SUBR		Varchar(003)	Null,
	FE_PROY_EJEC	Datetime		Null, 
	ST_DIGI_PROY	Varchar(001)	Null,
	CO_MONE		Varchar(003)	Null, 
	FE_DETA_DIGI	Datetime		Null, 
	DE_SUBR		Varchar(100)	Null,
	IM_SALD		Numeric(16,4)	Null,
	IM_DIGI		Numeric(16,4)	Null,
	IM_EJEC		Numeric(16,4)	Null)			----*/
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/* drop proc SP_TTSUBR_FINA_Q01  					----*/ 
/* grant all on SP_TTSUBR_FINA_Q01 to PUBLIC			----*/
/* SP_TTSUBR_FINA_Q01  '01', 'DOL', 'FTEERFA', '2004-01-01', '2004-01-01'   ----*/
/*---------------------------------------------------------*/
(
@ISCO_EMPR     	TD_VC_020,
@ISCO_MONE     	TD_VC_003,
@ISCO_MENU     	TD_VC_008,
@IDFE_PROY     	TD_DT_001,
@IDFE_PRIM_DIAS	TD_DT_001
)

AS
--- *** DECLARACION DE VARIABLES DE APOYO PARA EL POSTERIOR ALMACENAMIENTO EN LA TEMPORAL *** ---
Declare
	@CSCO_EMPR		TD_VC_020,
	@CSCO_SUBR		TD_VC_003,
	@CSDE_SUBR		TD_VC_100,

	@CSCO_SUBR_PROY	TD_VC_003,
	@CDNU_DIA1		TD_DT_001,
	@CDNU_DIA2		TD_DT_001,
	@CSPL_CONT		TD_VC_020,

	@VSCO_SUBR		TD_VC_003,
	@VNNU_ANNO		TD_IN_001,
	@VNNU_MESE		TD_IN_001,
	@VNNU_ACCI		TD_IN_001,
	@VSCO_MONE_DEFA	TD_VC_003,
	@VSCO_MONE_EXTR	TD_VC_003,
	@VSCO_TIAU_CCOS	TD_VC_001,
	@VSCO_TIAU_CLIE	TD_VC_001,
	@VSCO_TIAU_PROV	TD_VC_001,
	@VSCO_TIAU_EMPL	TD_VC_001,
	@VSTI_CAMB		TD_VC_003,
	@VSTI_ORIG		TD_VC_003,
	@VNNU_DIAS		TD_IN_001,
	@VNNU_DIAS_COUNT	TD_IN_001,
	@VDFE_DIAS_AUXI	TD_DT_001,
	@VDFE_DIUL_ANTE	TD_DT_001,
	@VDFE_DIAS_MESA	TD_DT_001
-------------------------------------------------------------------------------------------------
Begin
	--- *** CREACION DE LA TABLA TEMPORAL EN DONDE SE ALMACENARAN LOS DATOS FINALES *** ---
	CREATE TABLE #TWSUBR_FINA_Q01_1
	(
	CO_EMPR		Varchar(002)	Null,
	NU_ANNO		Int  		Null, 
	NU_MESE		Int  		Null,
	CO_SUBR		Varchar(003)	Null,
	FE_PROY_EJEC	Datetime		Null, 
	CO_MONE		Varchar(003)	Null, 
	IM_ORIG		Numeric(16,4)	Null,
	IM_CONV		Numeric(16,4)	Null,
	ST_EJEC		Varchar(001)	Null
	)

CREATE TABLE #TWSUBR_FINA_DIAS (
	CO_EMPR		Varchar(002)	Null,
	NU_ANNO		Int  		Null, 
	NU_MESE		Int  		Null,
	CO_SUBR		Varchar(003)	Null,
	FE_PROY_EJEC	Datetime		Null, 
	CO_MONE		Varchar(003)	Null, 
	FE_DETA_DIGI	Datetime		Null, 
	DE_SUBR		Varchar(100)	Null,
	IM_DIGI		Numeric(16,4)	Null,
	IM_EJEC		Numeric(16,4)	Null)

-------------------------------------------------------------------------------------------
--- *** OBTINE MONEDA NACIONAL Y EXTRAJERA, TIPO DE CAMBIO x PANTALLA O PARAMETROS *** ---	
	Select 	@VSCO_MONE_DEFA = t1.CO_MONE_DEFA, @VSCO_MONE_EXTR = t1.CO_MONE_EXTR,
			@VSTI_CAMB = IsNull(t2.TI_CAMB,t1.TI_CAMB), @VSTI_ORIG = IsNull(t2.TI_ORIG, t1.TI_ORIG),
			@VSCO_TIAU_PROV = t1.CO_TIAU_PROV,
			@VSCO_TIAU_CLIE = t1.CO_TIAU_CLIE,
			@VSCO_TIAU_CCOS = t1.CO_TIAU_CCOS,
			@VSCO_TIAU_EMPL = t1.CO_TIAU_EMPL
	From 	TMPARA_TESO t1
    LEFT OUTER JOIN TTFACA_OPCI t2
    ON  t1.CO_EMPR = t2.CO_EMPR  
    And t2.CO_MODU  = 'TEEM0001' 
    And t2.CO_MENU  = @ISCO_MENU     
	Where 	t1.CO_EMPR  = @ISCO_EMPR
			

--- *** DECLARACION DE VARIABLES PARA EL CURSOR CU_TTARUB_FINA_Q01_1 *** ---
	Declare CU_TTARUB_FINA_Q01_1 CURSOR FOR
	Select	CO_SUBR 
	From 	TTSUBR_FINA
	Where CO_EMPR = @ISCO_EMPR 
    And   ST_REPO = 'S'
	Order By NU_ORDE

--- *** DECLARACION DE VARIABLES PARA EL CURSOR CU_TTARUB_FINA_Q01_2 *** ---
	Declare CU_TTARUB_FINA_Q01_2 CURSOR FOR
	Select	CO_SUBR, NU_DIA1,  NU_DIA2, RTrim(LTrim(PL_CONT))
	From 	TTSUBR_PROY
	Where CO_EMPR = @ISCO_EMPR And 
		 AA_OPER = @VNNU_ANNO And 
		 MM_OPER = @VNNU_MESE


--- *** OBTINE EL NUMERO DE DIAS PARA GENERAR DIAS EN BLANCO VS RUBRO*** ---
	Select 	@VNNU_DIAS =  DateDiff ( Day, @IDFE_PRIM_DIAS, DateAdd(Day, -1, DateAdd(Month, 1, @IDFE_PRIM_DIAS))) + 1 ,
			@VNNU_DIAS_COUNT = 1

	select 	@VDFE_DIAS_AUXI = @IDFE_PRIM_DIAS


--- *** GENERAMOS A¾O Y MES DE LA OPERACION  *** ---
	Select 	@VNNU_ANNO =  Year(@IDFE_PROY),
			@VNNU_MESE = Month(@IDFE_PROY)

--- *** OBTINE EL MAXIMO DIA DEL MES ANTERIOR PARA LOS SALDOS DE LOS RUBRO*** ---
	Select	@VDFE_DIUL_ANTE = Max(FE_PROY_EJEC)
	From 	TWSUBR_FINA
	Where	CO_EMPR = @ISCO_EMPR And 
			NU_ANNO = DatePart(Year,DateAdd(Day, -1, @IDFE_PRIM_DIAS)) And 
			NU_MESE = DatePart(Month,DateAdd(Day, -1, @IDFE_PRIM_DIAS))

--- *** OBTINE EL MAXIMO DIA ANTERIOR PROYECTADO DEL MES ACTUAL *** ---
	Select	@VDFE_DIAS_MESA = Max(FE_PROY_EJEC)
	From 	TWSUBR_FINA
	Where	CO_EMPR = @ISCO_EMPR And 
			NU_ANNO = @VNNU_ANNO And 
			NU_MESE = @VNNU_MESE And 
			FE_PROY_EJEC < @IDFE_PROY


--- *** CURSOR GENERAR DIAS EN BLANCO CON CADA UNO DE LOS RUBROS *** ---
	Open CU_TTARUB_FINA_Q01_1
	Fetch CU_TTARUB_FINA_Q01_1 INTO @CSCO_EMPR, @CSCO_SUBR, @CSDE_SUBR
	WHILE @@Fetch_status = 0
	--- *** INICIO DE LA CAPTURA DE DATOS PARA CADA 	 A G R U P A M I E N T O  *** ---
	Begin
		
		WHILE @VNNU_DIAS_COUNT <= @VNNU_DIAS
		BEGIN
			Insert Into #TWSUBR_FINA_DIAS
				(CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR, FE_PROY_EJEC, 
				CO_MONE, FE_DETA_DIGI, 
				DE_SUBR, IM_DIGI, IM_EJEC )
			Values (@CSCO_EMPR, @VNNU_ANNO, @VNNU_MESE, @CSCO_SUBR, @IDFE_PROY, 
					@ISCO_MONE, @VDFE_DIAS_AUXI, 
					@CSDE_SUBR, 0, 0)

			Select @VDFE_DIAS_AUXI = DateAdd(Day, @VNNU_DIAS_COUNT, @IDFE_PRIM_DIAS )

			Select @VNNU_DIAS_COUNT = @VNNU_DIAS_COUNT + 1
		END

		---  ---		
		--- *** REINICIALIZAMOS LAS VARIABLES    *** ---
		Select 	@VDFE_DIAS_AUXI = @IDFE_PRIM_DIAS,
				@VNNU_DIAS_COUNT = 1


		Fetch CU_TTARUB_FINA_Q01_1 INTO @CSCO_EMPR, @CSCO_SUBR, @CSDE_SUBR
	End
	Close CU_TTARUB_FINA_Q01_1 
	Deallocate CU_TTARUB_FINA_Q01_1 


	
	/*---------------------------------------------------------*/
	/*--------- P  R  O  Y  E  C  T  A  D  O  --------  1   ---*/
	/*---------------------------------------------------------*/
	--- *** 1.1.- INSERTO TABLA TEMPORAL PROYECTADO DOCS PROVEDOR *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	(Select	t1.CO_EMPR, t1.CO_SUBR,	t1.FE_PROG_PAGO, @VNNU_ANNO, @VNNU_MESE,
			 t1.CO_MONE,
			 ( IsNull(t1.IM_TOTA,0) - IsNull(t1.IM_PAGA,0) ) , 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t1.CO_MONE,  @ISCO_MONE, t1.FE_PROG_PAGO, ( IsNull(t1.IM_TOTA,0) - IsNull(t1.IM_PAGA,0) ), 
@VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'N'
	 From 	TMDOCU_PROV t1
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_PROG_PAGO > @IDFE_PROY And
			Year (t1.FE_PROG_PAGO) = @VNNU_ANNO And
			Month(t1.FE_PROG_PAGO) = @VNNU_MESE And
			t1.CO_ESTA_DOCU != 'ANU'  And
			Len(Rtrim(ltrim(t1.CO_SUBR)))> 0)
	


--- *** CURSOR GENERAR IMPOETES PROYECTADOS DESDE R.R. H.H    *** ---
	Open CU_TTARUB_FINA_Q01_2
	Fetch CU_TTARUB_FINA_Q01_2 
	INTO  @CSCO_SUBR_PROY, 	@CDNU_DIA1, 	@CDNU_DIA2, 	@CSPL_CONT
	WHILE @@Fetch_status = 0

	Begin
		
		--- *** 1.2.- INSERTO TABLA TEMPORAL PROYECTADO A F P *** ---		
		If 	@CSPL_CONT = '@AFP'
		Begin	
			Insert Into #TWSUBR_FINA_Q01_1
			(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
			 CO_MONE,

			 IM_ORIG,	IM_CONV, ST_EJEC)
			( Select	@CSCO_EMPR, @CSCO_SUBR_PROY,	@CDNU_DIA1, @VNNU_ANNO, @VNNU_MESE,
						@VSCO_MONE_DEFA,
						sum(t2.NU_DATO_CALC),
						dbo.FN_TCFACT_CAMB_Q02 
						(@VSCO_MONE_DEFA,  @ISCO_MONE, @CDNU_DIA1, sum(t2.NU_DATO_CALC), @VSTI_CAMB, 
@VSTI_ORIG, 
						'N', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, 0),
					 'N'						
				From TMTRAB_CALC t1, TDTRAB_CALC t2, TTCNTR_PLAN t3
				Where t1.CO_EMPR = @ISCO_EMPR and t1.NU_ANNO = @VNNU_ANNO and t1.CO_UNID = '001' and 
				      t2.CO_TRAB = t1.CO_TRAB and t2.CO_EMPR = t1.CO_EMPR and
				      t2.CO_PLAN = t1.CO_PLAN and t2.NU_ANNO = t1.NU_ANNO and
				      t2.NU_PERI = t1.NU_PERI and t2.NU_CORR_PERI = t1.NU_CORR_PERI and
				     (t2.CO_CPTO_FORM = '@COVAR' or t2.CO_CPTO_FORM = '@SGINV' or t2.CO_CPTO_FORM = 
'@SPPEN') and
				      t3.CO_EMPR = t1.CO_EMPR and t3.CO_UNID = t1.CO_UNID and
				      t3.CO_PLAN = t1.CO_PLAN and t3.NU_ANNO = t1.NU_ANNO and
				      t3.NU_PERI = t1.NU_PERI and t3.NU_CORR_PERI = t1.NU_CORR_PERI and
				      t3.NU_MESE = @VNNU_MESE
				)		
			If @@error != 0  
			Begin  
				Close CU_TTARUB_FINA_Q01_2  
				Deallocate CU_TTARUB_FINA_Q01_2  
				Rollback Tran  
				Return  
			End  	
		End  	--- 'AFP'
	
		--- *** 1.3.- INSERTO TABLA TEMPORAL PROYECTADO   G R A T I F I C A C I O N  *** ---		
		If 	@CSPL_CONT = '@GRA'
		Begin	
	
			Insert Into #TWSUBR_FINA_Q01_1
			(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
			 CO_MONE,
			 IM_ORIG,	IM_CONV, ST_EJEC)
			( Select	@CSCO_EMPR, @CSCO_SUBR_PROY,	@CDNU_DIA1, @VNNU_ANNO, @VNNU_MESE,
						@VSCO_MONE_DEFA,
						sum(t2.NU_DATO_CALC),
						dbo.FN_TCFACT_CAMB_Q02 
						(@VSCO_MONE_DEFA,  @ISCO_MONE, Max(t3.FE_PAGO), sum(t2.NU_DATO_CALC), 
@VSTI_CAMB, @VSTI_ORIG, 
						'N', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, 0),
					 'N'						
				From TMTRAB_CALC t1, TDTRAB_CALC t2, TTCNTR_PLAN t3
				Where t1.CO_EMPR = @ISCO_EMPR and t1.NU_ANNO = @VNNU_ANNO and t1.CO_UNID = '001' and 
				      t2.CO_TRAB = t1.CO_TRAB and t2.CO_EMPR = t1.CO_EMPR and
				      t2.CO_PLAN = t1.CO_PLAN and t2.NU_ANNO = t1.NU_ANNO and
				      t2.NU_PERI = t1.NU_PERI and t2.NU_CORR_PERI = t1.NU_CORR_PERI and
				      t2.CO_CPTO_FORM = '@GRATI' and
				      t3.CO_EMPR = t1.CO_EMPR and t3.CO_UNID = t1.CO_UNID and
				      t3.CO_PLAN = t1.CO_PLAN and t3.NU_ANNO = t1.NU_ANNO and
				      t3.NU_PERI = t1.NU_PERI and t3.NU_CORR_PERI = t1.NU_CORR_PERI and
				      t3.NU_MESE = @VNNU_MESE
				)
	
			If @@error != 0  
			Begin  
				Close CU_TTARUB_FINA_Q01_2  
				Deallocate CU_TTARUB_FINA_Q01_2  
				Rollback Tran  
				Return  
			End  	
		End  	--- 'GRA'		
		
		--- *** 1.4.- INSERTO TABLA TEMPORAL PROYECTADO   N E T O   P L A N I L L A  *** ---		
		If 	@CSPL_CONT = '@NET'
		Begin	
	
			Insert Into #TWSUBR_FINA_Q01_1
			(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
			 CO_MONE,
			 IM_ORIG,	IM_CONV, ST_EJEC)
			( Select	@CSCO_EMPR, @CSCO_SUBR_PROY,	@CDNU_DIA1, @VNNU_ANNO, @VNNU_MESE,
						@VSCO_MONE_DEFA,
						(Sum(patindex(t2.TI_CPTO, 'I') * isnull(t2.NU_DATO_CALC, 0)) -
			       		 Sum(patindex(t2.TI_CPTO, 'D') * isnull(t2.NU_DATO_CALC, 0))),
						dbo.FN_TCFACT_CAMB_Q02 
						(@VSCO_MONE_DEFA,  @ISCO_MONE, Max(t3.FE_PAGO), 
						(Sum(patindex(t2.TI_CPTO, 'I') * isnull(t2.NU_DATO_CALC, 0)) -
			       		 Sum(patindex(t2.TI_CPTO, 'D') * isnull(t2.NU_DATO_CALC, 0))), @VSTI_CAMB, 
@VSTI_ORIG, 
						'N', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, 0),
					 'N'						
			From TMTRAB_CALC t1, TDTRAB_CALC t2, TTCNTR_PLAN t3
			Where t1.CO_EMPR = @ISCO_EMPR and
			      t1.CO_UNID = '001' and
			      t1.NU_ANNO = @VNNU_ANNO and
			      t2.CO_TRAB = t1.CO_TRAB and
			      t2.CO_EMPR = t1.CO_EMPR and
			      t2.CO_PLAN = t1.CO_PLAN and
			      t2.NU_ANNO = t1.NU_ANNO and
			      t2.NU_PERI = t1.NU_PERI and
			      t2.NU_CORR_PERI = t1.NU_CORR_PERI and
			      t3.CO_EMPR = t1.CO_EMPR and
			      t3.CO_UNID = t1.CO_UNID and 

			      t3.CO_PLAN = t1.CO_PLAN and
			      t3.NU_ANNO = t1.NU_ANNO and
			      t3.NU_PERI = t1.NU_PERI and
			      t3.NU_CORR_PERI = t1.NU_CORR_PERI and
			      t3.NU_MESE = @VNNU_MESE and
			      (t2.TI_CPTO = 'I' or t2.TI_CPTO = 'D') and
			      t2.ST_PRES_BOLE = 'S'	
				)
			If @@error != 0  
			Begin  
				Close CU_TTARUB_FINA_Q01_2  
				Deallocate CU_TTARUB_FINA_Q01_2  
				Rollback Tran  
				Return  
			End  	
		End  	--- '@NET'
		
		--- *** 1.5.- INSERTO TABLA TEMPORAL PROYECTADO   C T S   M E N S U A L  *** ---		
		If 	@CSPL_CONT = '@CTS'
		Begin
	
			Insert Into #TWSUBR_FINA_Q01_1
			(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
			 CO_MONE,
			 IM_ORIG,	IM_CONV, ST_EJEC)
			( Select	@CSCO_EMPR, @CSCO_SUBR_PROY,	@CDNU_DIA1, @VNNU_ANNO, @VNNU_MESE,
						@VSCO_MONE_DEFA,
						sum(t1.IM_PROV_MENS),
						dbo.FN_TCFACT_CAMB_Q02 
						(@VSCO_MONE_DEFA,  @ISCO_MONE, Max(t3.FE_PAGO), 
						sum(t1.IM_PROV_MENS), @VSTI_CAMB, @VSTI_ORIG, 
						'N', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, 0),
					 'N'						
				From TDPROV_CTSS t1, TMTRAB_CALC t2, TTCNTR_PLAN t3
				Where t1.CO_EMPR = @ISCO_EMPR and t1.NU_ANNO = @VNNU_ANNO and t1.NU_MESE = @VNNU_MESE and 
t1.NU_CORR_PROV = 1 and 
				     (t1.TI_PROV = 'CTM' or t1.TI_PROV = 'CTS') and
				      t3.CO_EMPR = t1.CO_EMPR and
				      t3.NU_ANNO = t1.NU_ANNO and
				      t3.NU_MESE = t1.NU_MESE and
				      t3.NU_CORR_PERI = t1.NU_CORR_PROV and
				      t2.CO_TRAB = t1.CO_TRAB and
				      t2.CO_EMPR = t1.CO_EMPR and
				      t2.CO_UNID = '001' and 
				      t2.CO_EMPR = t3.CO_EMPR and
				      t2.CO_UNID = t3.CO_UNID and
				      t2.CO_PLAN = t3.CO_PLAN and
				      t2.NU_ANNO = t3.NU_ANNO and
				      t2.NU_PERI = t3.NU_PERI and
				      t2.NU_CORR_PERI = t3.NU_CORR_PERI		)
	
			If @@error != 0  
			Begin  
				Close CU_TTARUB_FINA_Q01_2  
				Deallocate CU_TTARUB_FINA_Q01_2  
				Rollback Tran  
				Return  
			End  	
		End  	--- '@CTS'
	

		Fetch CU_TTARUB_FINA_Q01_2 
		INTO @CSCO_SUBR_PROY, 	@CDNU_DIA1, 	@CDNU_DIA2, 	@CSPL_CONT
	End
	Close CU_TTARUB_FINA_Q01_2
	Deallocate CU_TTARUB_FINA_Q01_2
		                
	
	/*---------------------------------------------------------*/
	/*--------- E  J  E  C  U  T  A  D  O  --------  2   ------*/
	/*---------------------------------------------------------*/
	--- *** 2.1.- INSERTO TABLA TEMPORAL EJECUTADO BANCOS SOLO CUENTAS *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	
	(Select	t1.CO_EMPR, t2.CO_SUBR,	t1.FE_OPER, @VNNU_ANNO, @VNNU_MESE,
			 t4.CO_MONE,
			 t1.IM_MOVI, 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t4.CO_MONE,  @ISCO_MONE, t1.FE_OPER, t1.IM_MOVI, @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'S'
	 From 	TCMOVI_BANC t1, TDMOVI_BANC t2, 
			TMCNTA_BANC t4
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_OPER <= @IDFE_PROY And
			Year (t1.FE_OPER) = @VNNU_ANNO And
			Month(t1.FE_OPER) = @VNNU_MESE And
			t1.TI_SITU != 'ANU'	And	 

			Len(Rtrim(ltrim(t2.CO_SUBR)))> 0 And
	
			t1.CO_EMPR = t2.CO_EMPR And
			t1.CO_UNID_CONC = t2.CO_UNID_CONC And
			t1.CO_BANC = t2.CO_BANC And 
			t1.NU_CNTA_BANC = t2.NU_CNTA_BANC And 
			t1.AA_BNCO = t2.AA_BNCO And
			t1.MM_BNCO = t2.MM_BNCO And 
			t1.TI_MOVI_BANC = t2.TI_MOVI_BANC And 
			t1.NU_CNTA_BANC = t2.NU_CNTA_BANC And 
	
			Len(Rtrim(ltrim(t2.CO_CNTA_EMPR)))> 0 And
			Len(Rtrim(ltrim(t2.TI_DOCU_ENTI)))= 0 And
			Len(Rtrim(ltrim(t2.NU_DOCU_ENTI)))= 0 And
			t1.CO_EMPR = t4.CO_EMPR And
			t1.CO_BANC = t4.CO_BANC And
			t1.NU_CNTA_BANC = t4.NU_CNTA_BANC )
	
	---  ---		
	--- *** 2.2.- INSERTO TABLA TEMPORAL EJECUTADO BANCOS DOCUMENTOS *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	
	(Select	t1.CO_EMPR,
			Case t2.TI_ENTI 
				When @VSCO_TIAU_PROV Then t5.CO_SUBR
				Else t2.CO_SUBR
			End,	t1.FE_OPER, @VNNU_ANNO, @VNNU_MESE,
			 t4.CO_MONE,
			 t1.IM_MOVI, 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t4.CO_MONE,  @ISCO_MONE, t1.FE_OPER, t1.IM_MOVI, @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'S'
	 From 	TCMOVI_BANC t1, TDMOVI_BANC t2, 
			TMCNTA_BANC t4,
			TMDOCU_PROV t5
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_OPER <= @IDFE_PROY And
			Year (t1.FE_OPER) = @VNNU_ANNO And
			Month(t1.FE_OPER) = @VNNU_MESE And
			t1.TI_SITU != 'ANU'	And	 

			( ( t2.TI_ENTI = @VSCO_TIAU_PROV  And  Len(Rtrim(ltrim(t5.CO_SUBR)))> 0 ) Or ( 
Len(Rtrim(ltrim(t2.CO_SUBR)))> 0 ) ) And	

			t1.CO_EMPR = t2.CO_EMPR And
			t1.CO_UNID_CONC = t2.CO_UNID_CONC And
			t1.CO_BANC = t2.CO_BANC And 
			t1.NU_CNTA_BANC = t2.NU_CNTA_BANC And 
			t1.AA_BNCO = t2.AA_BNCO And
			t1.MM_BNCO = t2.MM_BNCO And 
			t1.TI_MOVI_BANC = t2.TI_MOVI_BANC And 
			t1.NU_CNTA_BANC = t2.NU_CNTA_BANC And 
	
			Len(Rtrim(ltrim(t2.CO_CNTA_EMPR)))= 0 And
			Len(Rtrim(ltrim(t2.TI_DOCU_ENTI)))> 0 And
			Len(Rtrim(ltrim(t2.NU_DOCU_ENTI)))> 0 And
			t1.CO_EMPR = t4.CO_EMPR And
			t1.CO_BANC = t4.CO_BANC And
			t1.NU_CNTA_BANC = t4.NU_CNTA_BANC And
	
			t2.CO_EMPR = t5.CO_EMPR And
			t2.CO_ENTI = t5.CO_PROV And
			t2.TI_DOCU_ENTI = t5.CO_TIPO_DOCU And
			t2.NU_DOCU_ENTI = t5.NU_DOCU_PROV And
			t5.CO_ESTA_DOCU != 'ANU' )
	
	--- *** 2.3.- INSERTO TABLA TEMPORAL EJECUTADO BANCOS SOLO CABECERA *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	
	(Select	t1.CO_EMPR, t1.CO_SUBR,	t1.FE_OPER, @VNNU_ANNO, @VNNU_MESE,
			 t4.CO_MONE,
			 t1.IM_MOVI, 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t4.CO_MONE,  @ISCO_MONE, t1.FE_OPER, t1.IM_MOVI, @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'S'
	 From 	TCMOVI_BANC t1, 
			TMCNTA_BANC t4
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_OPER <= @IDFE_PROY And
			Year (t1.FE_OPER) = @VNNU_ANNO And
			Month(t1.FE_OPER) = @VNNU_MESE And
			t1.TI_SITU != 'ANU'	And	 

			Len(Rtrim(ltrim(t1.CO_SUBR)))> 0 And

			Not Exists (	Select	t2.CO_EMPR
						From		TDMOVI_BANC t2
						Where	t1.CO_EMPR = t2.CO_EMPR And
								t1.CO_UNID_CONC = t2.CO_UNID_CONC And 
								t1.CO_BANC = t2.CO_BANC And 
								t1.NU_CNTA_BANC = t2.NU_CNTA_BANC And 
								t1.AA_BNCO = t2.AA_BNCO And
								t1.MM_BNCO = t2.MM_BNCO And 
								t1.TI_MOVI_BANC = t2.TI_MOVI_BANC And 
								t1.NU_CNTA_BANC = t2.NU_CNTA_BANC)And
			t1.CO_EMPR = t4.CO_EMPR And
			t1.CO_BANC = t4.CO_BANC And
			t1.NU_CNTA_BANC = t4.NU_CNTA_BANC )


	/*---------------------------------------------------------*/
	/*--------- E  J  E  C  U  T  A  D  O  --------  3   ------*/
	/*---------------------------------------------------------*/
	--- *** 3.1.- INSERTO TABLA TEMPORAL EJECUTADO CAJA SOLO CUENTAS *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	
	(Select	t1.CO_EMPR, t2.CO_SUBR,	t1.FE_OPER, @VNNU_ANNO, @VNNU_MESE,
			 t4.CO_MONE,
			 t1.IM_MOVI, 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t4.CO_MONE,  @ISCO_MONE, t1.FE_OPER, t1.IM_MOVI, @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'S'
	 From 	TCMOVI_CAJA t1, TDMOVI_CAJA t2, 
			TMCAJA t4
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_OPER <= @IDFE_PROY And
			Year (t1.FE_OPER) = @VNNU_ANNO And
			Month(t1.FE_OPER) = @VNNU_MESE And
			t1.TI_SITU != 'ANU'	And	 

			Len(Rtrim(ltrim(t2.CO_SUBR)))> 0 And
	
			t1.CO_EMPR = t2.CO_EMPR And
			t1.CO_CAJA = t2.CO_CAJA And 
			t1.AA_CAJA = t2.AA_CAJA And
			t1.MM_CAJA = t2.MM_CAJA And 
			t1.TI_MOVI = t2.TI_MOVI And 
	
			Len(Rtrim(ltrim(t2.CO_CNTA_EMPR)))> 0 And
			Len(Rtrim(ltrim(t2.TI_DOCU_ENTI)))= 0 And
			Len(Rtrim(ltrim(t2.NU_DOCU_ENTI)))= 0 And
			t1.CO_EMPR = t4.CO_EMPR And
			t1.CO_CAJA = t4.CO_CAJA  )
	
	---  ---		
	--- *** 3.2.- INSERTO TABLA TEMPORAL EJECUTADO CAJA DOCUMENTOS *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	
	(Select	t1.CO_EMPR,
			Case t2.TI_ENTI 
				When @VSCO_TIAU_PROV Then t5.CO_SUBR
				Else t2.CO_SUBR
			End,	t1.FE_OPER, @VNNU_ANNO, @VNNU_MESE,
			 t4.CO_MONE,
			 t1.IM_MOVI, 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t4.CO_MONE,  @ISCO_MONE, t1.FE_OPER, t1.IM_MOVI, @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'S'
	 From 	TCMOVI_CAJA t1, TDMOVI_CAJA t2, 
			TMCAJA t4,
			TMDOCU_PROV t5
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_OPER <= @IDFE_PROY And
			Year (t1.FE_OPER) = @VNNU_ANNO And
			Month(t1.FE_OPER) = @VNNU_MESE And
			t1.TI_SITU != 'ANU'	And	 
				( ( t2.TI_ENTI = @VSCO_TIAU_PROV  And  Len(Rtrim(ltrim(t5.CO_SUBR)))> 0 ) Or ( 
Len(Rtrim(ltrim(t2.CO_SUBR)))> 0 ) ) And	
	
			t1.CO_EMPR = t2.CO_EMPR And
			t1.CO_CAJA = t2.CO_CAJA And 
			t1.AA_CAJA = t2.AA_CAJA And
			t1.MM_CAJA = t2.MM_CAJA And 
			t1.TI_MOVI = t2.TI_MOVI And 
	
			Len(Rtrim(ltrim(t2.CO_CNTA_EMPR)))= 0 And
			Len(Rtrim(ltrim(t2.TI_DOCU_ENTI)))> 0 And
			Len(Rtrim(ltrim(t2.NU_DOCU_ENTI)))> 0 And
			t1.CO_EMPR = t4.CO_EMPR And
			t1.CO_CAJA = t4.CO_CAJA And
	
			t2.CO_EMPR = t5.CO_EMPR And
			t2.CO_ENTI = t5.CO_PROV And
			t2.TI_DOCU_ENTI = t5.CO_TIPO_DOCU And
			t2.NU_DOCU_ENTI = t5.NU_DOCU_PROV And
			t5.CO_ESTA_DOCU != 'ANU' )
	
	--- *** 3.3.- INSERTO TABLA TEMPORAL EJECUTADO CAJA SOLO CABECERA *** ---		
	Insert Into #TWSUBR_FINA_Q01_1
	(CO_EMPR,	CO_SUBR,	FE_PROY_EJEC,	NU_ANNO,	NU_MESE,
	 CO_MONE,
	 IM_ORIG,	IM_CONV, ST_EJEC)
	
	(Select	t1.CO_EMPR, t1.CO_SUBR,	t1.FE_OPER, @VNNU_ANNO, @VNNU_MESE,
			 t4.CO_MONE,
			 t1.IM_MOVI, 
			 dbo.FN_TCFACT_CAMB_Q02 
				(t4.CO_MONE,  @ISCO_MONE, t1.FE_OPER, t1.IM_MOVI, @VSTI_CAMB, @VSTI_ORIG, 
				'S', @VSCO_MONE_DEFA, @VSCO_MONE_EXTR, t1.FA_TIPO_CAMB),
			 'S'
	 From 	TCMOVI_CAJA t1, 
			TMCAJA t4
	 Where 	t1.CO_EMPR = @ISCO_EMPR And 
			t1.FE_OPER <= @IDFE_PROY And
			Year (t1.FE_OPER) = @VNNU_ANNO And
			Month(t1.FE_OPER) = @VNNU_MESE And
			t1.TI_SITU != 'ANU'	And	 

			Len(Rtrim(ltrim(t1.CO_SUBR)))> 0 And

			Not Exists (	Select	t2.CO_EMPR
						From		TDMOVI_CAJA t2
						Where	t1.CO_EMPR = t2.CO_EMPR And
								t1.CO_CAJA = t2.CO_CAJA And 
								t1.AA_CAJA = t2.AA_CAJA And
								t1.MM_CAJA = t2.MM_CAJA And 
								t1.TI_MOVI = t2.TI_MOVI )And
			t1.CO_EMPR = t4.CO_EMPR And
			t1.CO_CAJA = t4.CO_CAJA )


---****************************************************************************************************--



--- *** ELIMINAMOS DE LA TABLA DE TRABAJO (BACKUP)   *** ---
	Delete #TWSUBR_FINA

--- *** INSERTAMOS EN LA TABLA DE TRABAJO (BACKUP)   *** ---
		Insert Into #TWSUBR_FINA
			(CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR, FE_PROY_EJEC, 
			CO_MONE, FE_DETA_DIGI, ST_DIGI_PROY, 
			IM_DIGI, IM_EJEC )
		(
		---- Lo Nuevo Proyectado Refrescar----
		Select 
			CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR, Max(@IDFE_PROY), 
			Max(@ISCO_MONE), FE_PROY_EJEC, 'P',
			0, IsNull(Sum(IsNull(IM_CONV,0)),0)
		From 	#TWSUBR_FINA_Q01_1
		Where ST_EJEC = 'N'
		Group By CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR , FE_PROY_EJEC
		---- Lo ejecutado si es Nuevo Fecha de Proyeccion ----
		Union All

		Select 
			CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR, Max(@IDFE_PROY), 
			Max(@ISCO_MONE), FE_PROY_EJEC, 'P',
			0, IsNull(Sum(IsNull(IM_CONV,0)),0)
		From 	#TWSUBR_FINA_Q01_1
		Where ST_EJEC = 'S' And
		Not Exists (	Select	TWSUBR_FINA.CO_EMPR
					From 	TWSUBR_FINA
					Where	TWSUBR_FINA.CO_EMPR = #TWSUBR_FINA_Q01_1.CO_EMPR And 
							TWSUBR_FINA.NU_ANNO = #TWSUBR_FINA_Q01_1.NU_ANNO And 
							TWSUBR_FINA.NU_MESE = #TWSUBR_FINA_Q01_1.NU_MESE And 	
							TWSUBR_FINA.FE_PROY_EJEC = @IDFE_PROY)
		Group By CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR , FE_PROY_EJEC
		---- Lo Grabado Fecha Actual----
		Union All

		Select	CO_EMPR, NU_ANNO, NU_MESE,CO_SUBR, FE_PROY_EJEC,
				@ISCO_MONE, FE_DETA_DIGI, 'D',
				IM_DIGI, IM_EJEC
		From 	TWSUBR_FINA
		Where	CO_EMPR = @ISCO_EMPR And
				NU_ANNO = @VNNU_ANNO And
				NU_MESE = @VNNU_MESE And
				FE_PROY_EJEC = @IDFE_PROY
		---- Lo Grabado e Ingresado en Fecha Anterior----
		Union All

		Select	CO_EMPR, NU_ANNO, NU_MESE,CO_SUBR, @IDFE_PROY,
				@ISCO_MONE, FE_DETA_DIGI, 'D',
				IM_DIGI, 0
		From 	TWSUBR_FINA
		Where	CO_EMPR = @ISCO_EMPR And
				NU_ANNO = @VNNU_ANNO And
				NU_MESE = @VNNU_MESE And
				FE_PROY_EJEC = @VDFE_DIAS_MESA And
				FE_DETA_DIGI >= @IDFE_PROY And
		Not Exists (	Select	TWSUBR_FINA.CO_EMPR
					From 	TWSUBR_FINA
					Where	TWSUBR_FINA.CO_EMPR = @ISCO_EMPR And 
							TWSUBR_FINA.NU_ANNO = @VNNU_ANNO And 
							TWSUBR_FINA.NU_MESE = @VNNU_MESE And 	
							TWSUBR_FINA.FE_PROY_EJEC = @IDFE_PROY)
		)	

--- *** INSERTAMOS LA TABLA TEMPORAL DE DIAS... *** ---
		Insert Into #TWSUBR_FINA
			(CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR, FE_PROY_EJEC, 

			CO_MONE, FE_DETA_DIGI, ST_DIGI_PROY, 
			IM_DIGI, IM_EJEC )

		(
		Select 
			CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR, FE_PROY_EJEC, 
				CO_MONE, FE_DETA_DIGI, 'P',
				IM_DIGI, IM_EJEC
		From 	#TWSUBR_FINA_DIAS
		Where	CO_EMPR = @ISCO_EMPR And 
				NU_ANNO = @VNNU_ANNO And 
				NU_MESE = @VNNU_MESE And 	
				FE_PROY_EJEC = @IDFE_PROY		 
		And Not Exists (	Select	#TWSUBR_FINA.CO_EMPR
					From 	#TWSUBR_FINA
					Where	#TWSUBR_FINA.CO_EMPR = #TWSUBR_FINA_DIAS.CO_EMPR And 
							#TWSUBR_FINA.NU_ANNO = #TWSUBR_FINA_DIAS.NU_ANNO And 
							#TWSUBR_FINA.NU_MESE = #TWSUBR_FINA_DIAS.NU_MESE And 
							#TWSUBR_FINA.CO_SUBR = #TWSUBR_FINA_DIAS.CO_SUBR And 
							#TWSUBR_FINA.FE_PROY_EJEC = #TWSUBR_FINA_DIAS.FE_PROY_EJEC And 
							#TWSUBR_FINA.FE_DETA_DIGI = #TWSUBR_FINA_DIAS.FE_DETA_DIGI  )
		)

--- *** ACTULALIZAMOS SALDOS A LA TABLA DE TRABAJO (BACKUP)   *** ---
		Update #TWSUBR_FINA
		Set
		#TWSUBR_FINA.IM_SALD = (
					 Select	Sum(IsNull(TWSUBR_FINA.IM_DIGI,0) + IsNull(TWSUBR_FINA.IM_EJEC,0))
					 From 	TWSUBR_FINA,
							(
							Select	CO_EMPR CO_EMPR, NU_ANNO NU_ANNO,
									NU_MESE NU_MESE, CO_SUBR CO_SUBR,
									Max(FE_PROY_EJEC) FE_PROY_EJEC
							From		TWSUBR_FINA
							Where	CO_EMPR  = @ISCO_EMPR And
									NU_ANNO <= DatePart(Year ,DateAdd(Day, -1, 
@IDFE_PRIM_DIAS)) And 
									NU_MESE <= DatePart(Month,DateAdd(Day, -1, 
@IDFE_PRIM_DIAS))
							Group By 	CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR
							) t3	 --- Tabla de Fechas Maximas Para los Saldos
					 Where	TWSUBR_FINA.CO_EMPR = #TWSUBR_FINA.CO_EMPR	And
							TWSUBR_FINA.CO_SUBR = #TWSUBR_FINA.CO_SUBR	And
							TWSUBR_FINA.CO_EMPR = t3.CO_EMPR And
							TWSUBR_FINA.NU_ANNO = t3.NU_ANNO And
							TWSUBR_FINA.NU_MESE = t3.NU_MESE And
							TWSUBR_FINA.CO_SUBR = t3.CO_SUBR And
							TWSUBR_FINA.FE_PROY_EJEC = t3.FE_PROY_EJEC  )


		Where #TWSUBR_FINA.CO_EMPR = @ISCO_EMPR	And
			#TWSUBR_FINA.NU_ANNO = @VNNU_ANNO And
			#TWSUBR_FINA.NU_MESE = @VNNU_MESE And
			#TWSUBR_FINA.FE_PROY_EJEC = @IDFE_PROY And
			Exists	(
					 Select	TWSUBR_FINA.CO_EMPR
					 From 	TWSUBR_FINA,
							(
							Select	CO_EMPR CO_EMPR, NU_ANNO NU_ANNO,
									NU_MESE NU_MESE, CO_SUBR CO_SUBR,
									Max(FE_PROY_EJEC) FE_PROY_EJEC
							From		TWSUBR_FINA
							Group By 	CO_EMPR, NU_ANNO, NU_MESE, CO_SUBR
							) t3		 --- Tabla de Fechas Maximas Para los Saldos
					 Where	TWSUBR_FINA.CO_EMPR = #TWSUBR_FINA.CO_EMPR	And
							TWSUBR_FINA.CO_SUBR = #TWSUBR_FINA.CO_SUBR	And
							TWSUBR_FINA.CO_EMPR = t3.CO_EMPR And
							TWSUBR_FINA.NU_ANNO = t3.NU_ANNO And
							TWSUBR_FINA.NU_MESE = t3.NU_MESE And
							TWSUBR_FINA.CO_SUBR = t3.CO_SUBR And
							TWSUBR_FINA.FE_PROY_EJEC = t3.FE_PROY_EJEC )



--- *** ACTUALIZAMOS LOS IMPORTES DIGITADOS PARA LOS PROYECTADO EN LA TABL D TRABAJO (BACKUP)   *** ---
		Update #TWSUBR_FINA
		Set
		#TWSUBR_FINA.IM_DIGI = (
					 Select	IsNull(TWSUBR_FINA.IM_DIGI,0)
					 From 	TWSUBR_FINA
					 Where	TWSUBR_FINA.CO_EMPR = #TWSUBR_FINA.CO_EMPR	And
							TWSUBR_FINA.NU_ANNO = #TWSUBR_FINA.NU_ANNO And
							TWSUBR_FINA.NU_MESE = #TWSUBR_FINA.NU_MESE And
							TWSUBR_FINA.CO_SUBR = #TWSUBR_FINA.CO_SUBR And
							TWSUBR_FINA.FE_PROY_EJEC = #TWSUBR_FINA.FE_PROY_EJEC And
							TWSUBR_FINA.FE_DETA_DIGI = #TWSUBR_FINA.FE_DETA_DIGI )
		Where #TWSUBR_FINA.CO_EMPR = @ISCO_EMPR	And
			#TWSUBR_FINA.NU_ANNO = @VNNU_ANNO And
			#TWSUBR_FINA.NU_MESE = @VNNU_MESE And
			#TWSUBR_FINA.FE_PROY_EJEC = @IDFE_PROY And
			Exists	(
					 Select	TWSUBR_FINA.CO_EMPR
					 From 	TWSUBR_FINA
					 Where	TWSUBR_FINA.CO_EMPR = #TWSUBR_FINA.CO_EMPR	And
							TWSUBR_FINA.NU_ANNO = #TWSUBR_FINA.NU_ANNO And
							TWSUBR_FINA.NU_MESE = #TWSUBR_FINA.NU_MESE And
							TWSUBR_FINA.CO_SUBR = #TWSUBR_FINA.CO_SUBR And
							TWSUBR_FINA.FE_PROY_EJEC = #TWSUBR_FINA.FE_PROY_EJEC And
							TWSUBR_FINA.FE_DETA_DIGI = #TWSUBR_FINA.FE_DETA_DIGI )


--- *************************************************** ---
--- *** EJECUTAMOS EL SELECT QUE CARGARA LA TABLA   *** ---
--- *************************************************** ---

Select	#TWSUBR_FINA.CO_SUBR,
		TTSUBR_FINA.DE_SUBR, #TWSUBR_FINA.FE_PROY_EJEC, 
		Max(#TWSUBR_FINA.ST_DIGI_PROY),
		Max(IsNull(#TWSUBR_FINA.IM_SALD,0)) AS IM_SALD_ANTE,
		Sum (	PatIndex( '1', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '2', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '3', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '4', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '5', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '6', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '7', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '8', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '9', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '10', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '11', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '12', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '13', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '14', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '15', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '16', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '17', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '18', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '19', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '20', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '21', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '22', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '23', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '24', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '25', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '26', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '27', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '28', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '29', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '30', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) ,
		Sum (	PatIndex( '31', RTrim(Ltrim(Convert(Varchar(2),IsNull(DatePart (day, FE_DETA_DIGI),0)))) )* 
			(IsNull(IM_EJEC,0)+ IsNull(IM_DIGI,0))
			) 
From 	#TWSUBR_FINA , TTSUBR_FINA 
Where	#TWSUBR_FINA.CO_EMPR = @ISCO_EMPR And 
		#TWSUBR_FINA.NU_ANNO = @VNNU_ANNO And 
		#TWSUBR_FINA.NU_MESE = @VNNU_MESE And 	
		#TWSUBR_FINA.FE_PROY_EJEC = @IDFE_PROY And 
		#TWSUBR_FINA.CO_EMPR = TTSUBR_FINA.CO_EMPR And
		#TWSUBR_FINA.CO_SUBR = TTSUBR_FINA.CO_SUBR 
Group BY  #TWSUBR_FINA.CO_SUBR, TTSUBR_FINA.DE_SUBR, #TWSUBR_FINA.FE_PROY_EJEC
Order By  Max(TTSUBR_FINA.NU_ORDE)



End
/********************************************** F I N ********************************************************/


GO
